package logic

import (
	"context"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	"2106A-zg6/baidu/srv/internal/svc"
	"2106A-zg6/baidu/srv/models"
	"2106A-zg6/baidu/srv/pb/baidu"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetFileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetFileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileLogic {
	return &GetFileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetFileLogic) GetFile(in *baidu.GetFileRequest) (*baidu.GetFileResponse, error) {

	file, err := models.GetFile(in.UserId)
	if err != nil {
		return nil, status.Errorf(codes.InvalidArgument, err.Error())
	}

	var data []*baidu.File

	for _, v := range file {
		data = append(data, ModelRespFile(v))
	}

	return &baidu.GetFileResponse{
		Files: data,
	}, nil
}

func ModelRespFile(f models.File) *baidu.File {
	return &baidu.File{
		Id:     int64(f.ID),
		UserId: f.UserId,
		File:   f.File,
	}
}
